﻿cmake_minimum_required (VERSION 3.12)

if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

# By Zero123
project ("MCModScanner")

if(MSVC)
    add_compile_options(/utf-8)
    add_compile_options(/EHsc)
    # set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_compile_options(/MTd)
    else()
        add_compile_options(/MT)
    endif()
endif()

# 动态添加MD5哈希模块
function(ADD_MD5 targetName)
    target_sources(${targetName} PRIVATE "${CMAKE_SOURCE_DIR}/libs/hash_library/md5.cpp")
	target_include_directories(${targetName} PRIVATE "${CMAKE_SOURCE_DIR}/libs/hash_library")
endfunction()

# CURL库
# 禁用共享库构建（只构建静态库）
set(BUILD_SHARED_LIBS false)

# 禁用其他不需要的部分
set(BUILD_CURL_EXE false)
set(BUILD_EXAMPLES false)
set(BUILD_LIBCURL_DOCS false)
set(BUILD_MISC_DOCS false)

if (WIN32)
	# WINDOWS环境直接使用系统库处理SSL
	set(CURL_USE_SCHANNEL true)
endif()

# 引入curl库 (http/https请求支持)
add_subdirectory("libs/curl")

add_executable (MCModScanner "MCModScanner.cpp" "${CMAKE_SOURCE_DIR}/Res/EmbedRes.cpp" "Utils/EnvEncode.cpp" "UserManager.cpp")
ADD_MD5(MCModScanner)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
	target_link_directories(MCModScanner PRIVATE "${CMAKE_SOURCE_DIR}/libs/webui/debug")
	target_compile_definitions(MCModScanner PRIVATE _WEB_DEBUG_PATH="${CMAKE_SOURCE_DIR}/Res/Vue/dist")
else()
	target_link_directories(MCModScanner PRIVATE "${CMAKE_SOURCE_DIR}/libs/webui")
	if(WIN32)
		# 禁用控制台
		set_target_properties(MCModScanner PROPERTIES WIN32_EXECUTABLE TRUE)
		# set_target_properties(MCModScanner PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
	endif()
endif()

# CURL
target_include_directories(MCModScanner PRIVATE ${CURL_INCLUDE_DIRS})
target_link_libraries(MCModScanner PRIVATE CURL::libcurl)
# WebUI
target_include_directories(MCModScanner PRIVATE "${CMAKE_SOURCE_DIR}/libs/webui/include")
target_link_libraries(MCModScanner PRIVATE webui-2-static)
# Static Res
target_include_directories(MCModScanner PRIVATE "${CMAKE_SOURCE_DIR}/Res")
set_property(TARGET MCModScanner PROPERTY CXX_STANDARD 20)

# Test
add_executable(Test1 "UserManager.cpp" "Utils/EnvEncode.cpp")
ADD_MD5(Test1)
target_compile_definitions(Test1 PRIVATE _USER_MANAGER_TEST)
target_include_directories(Test1 PRIVATE ${CURL_INCLUDE_DIRS})
target_link_libraries(Test1 PRIVATE CURL::libcurl)
set_property(TARGET Test1 PROPERTY CXX_STANDARD 20)